/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mavenrentcars.service.impl;

import com.mavenrentcars.dao.PlaceDao;
import com.mavenrentcars.model.Place;
import com.mavenrentcars.service.PlaceService;
import java.util.HashMap;
import java.util.List;
import javax.annotation.Resource;
import org.json.simple.JSONObject;
import org.springframework.stereotype.Service;

/**
 *
 * @author serg
 */
@Service("placeService")
public class PlaceServiceImpl implements PlaceService {

    @Resource(name = "placeDao")
    PlaceDao placeDao;

    @Override
    public Place save(Place model) {
        return placeDao.save(model);
    }

    @Override
    public Place get(Integer id) {
        return placeDao.get(id);
    }

    @Override
    public void remove(Integer id) {
        placeDao.remove(id);
    }

    @Override
    public List<Place> getAll() {
        return placeDao.getAll();
    }

    @Override
    public String getJsonPlaces() {

        JSONObject jsonObject = new JSONObject();
        List<Place> places = getAll();
        for (Place place : places) {
            HashMap<String, String> pmap = new HashMap<String, String>();
            pmap.put("id", Integer.toString(place.getId()));
            pmap.put("name", place.getPlaceName());
            pmap.put("action", "add");
            jsonObject.put(pmap.get("id"), pmap);
        }
        return jsonObject.toString();
    }
}
